//
//  BaseVC.m
//  质检
//
//  Created by Mac on 2018/3/28.
//  Copyright © 2018年 158682312@qq.com. All rights reserved.
//

#import "BaseVC.h"
#import "WKFNetwork.h"

@interface BaseVC ()

@end

@implementation BaseVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
//    if (IOS7_OR_LATER) {
//        [self setNeedsStatusBarAppearanceUpdate];
//        self.automaticallyAdjustsScrollViewInsets = NO;
//        self.extendedLayoutIncludesOpaqueBars = NO;
//        self.modalPresentationCapturesStatusBarAppearance = NO;
//        self.navigationController.navigationBar.translucent = NO;
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//        IOS6_7_DELTA(self.view,0, 20, 0, -20);
//        
//    }
//    
//    
//    //滑动返回
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
#endif
    
    /**  导航栏颜色
     */
//    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //    [self.navigationController.navigationBar setBackgroundImage:[KNHelper imageWithColor:uColorMainBlue] forBarMetrics:UIBarMetricsDefault];
}
- (void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:animated];
    
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}



#pragma mark - AFNetwork
//开始网络请求
- (void)afNetworkBeginWithHost:(NSString *)aHostName path:(NSString *)aPath params:(NSDictionary *)aParams httpMethod:(NSString *)aHttpMethod selectorName:(NSString *)aSelectorName
{
    [[WKFNetwork sharedSingleton] afNetworkBeginWithHost:aHostName path:aPath params:aParams httpMethod:aHttpMethod selectorName:aSelectorName delegate:self];
}



#pragma mark - Network success
//网络请求成功
- (void)networkSuccessWithResponseDic:(NSDictionary *)responseDic networkPath:(NSString *)aNetworkPath statusModel:(ZXTAPIStatus *)status
{
    if (![DataCheck isValidDictionary:responseDic])
    {
        //如果返回的是坏数据
        [EPProgressHUD dismiss];
        DLog(@" ----- 网络请求返回错误 数据");
    }
}


#pragma mark - Network error
//API 请求失败
- (void)networkAPIError:(NSDictionary *)responseDic networkPath:(NSString *)aNetworkPath statusModel:(ZXTAPIStatus *)status
{
    
}
//网络错误
- (void)networkError:(NSError *)error
         networkPath:(NSString *)aNetworkPath
{
    [self showErrorHUD:error];
}

- (void)showErrorHUD:(NSError *)error {
    switch (error.code)
    {
        case NSURLErrorTimedOut:
            [EPProgressHUD showErrorWithStatus:@"加载失败,网络超时" maskType:SVProgressHUDMaskTypeGradient];
            break;
        case NSURLErrorNotConnectedToInternet:
            [EPProgressHUD showErrorWithStatus:@"网络未连接,请检查网络设置。" maskType:SVProgressHUDMaskTypeGradient];
            break;
        case NSURLErrorCannotConnectToHost:
            [EPProgressHUD showErrorWithStatus:@"网络连接断开" maskType:SVProgressHUDMaskTypeGradient];
            break;
        default:
            [EPProgressHUD showErrorWithStatus:@"网络连接断开" maskType:SVProgressHUDMaskTypeGradient];
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



// 路径
- (NSString *)getFilePathWithImageName:(NSString *)imageName
{
    NSFileManager *fileManager = [[NSFileManager alloc] init];

    NSString *pathDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSDictionary * selfdic =  [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"myDictionary"];

    NSString *createPath = [NSString stringWithFormat:@"%@/", pathDocuments];
    
    // 判断文件夹是否存在，如果不存在，则创建

    if (![[NSFileManager defaultManager] fileExistsAtPath:createPath]) {

        [fileManager createDirectoryAtPath:createPath withIntermediateDirectories:YES attributes:nil error:nil];

    } else {

        NSLog(@"FileDir is exists.");

    }

    

    if (imageName) {

        NSString *filePath =  [createPath stringByAppendingString:imageName];
        return filePath;

    }

    return nil;

}

// 下载

-(void)DownloadImageViewByUrl:(NSString*)url andImageName:(NSString*)imageName

{

        NSString *filePath = [self getFilePathWithImageName:[NSString stringWithFormat:@"%@",imageName]];

        UIImage * iscunzai = [UIImage imageWithContentsOfFile:filePath];

        if (iscunzai) {

            return ;

        }

        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];

        UIImage *image = [UIImage imageWithData:data];

        // 保存文件的名称
    NSLog(@"%@",filePath);

        if ([UIImagePNGRepresentation(image) writeToFile:filePath atomically:YES]) {// 保存成功

            NSLog(@"保存成功");

        }else{

            NSLog(@"保存失败");

        }

 

}


@end
